<?php

namespace App\Http\Controllers\Api\v1;

use App\common;
use App\Http\Controllers\Controller;
use App\Logic\SignLogic;
use Illuminate\Http\Request;

class SignController extends Controller
{
    /**
     * 签到
     * @param Request $request
     * @return array|false|string
     * @yuankai
     */
    public function sign(Request $request)
    {
        try {
            $params = $request->post();
            if(empty($params['uid'])) fail('参数错误','null');
            SignLogic::sign($params);
            return common::success('签到成功','null');
        }catch (\Exception $exception){
            return common::fail($exception->getMessage(),'null');
        }
    }

    /**
     * 查看是否签到
     * @param Request $request
     * @yuankai
     */
    public function getSign(Request $request)
    {
        $params = $request->get();
        $res = SignLogic::getSign($params['uid'],$params['date']);//查看是否签到
        if($res){
            return common::success('您已签到','null');
        }else{
            return common::fail('请签到','null');
        }
    }

    /**
     * 查看本月签到天数
     * @param Request $request
     * @yuankai
     */
    public function getCount(Request $request)
    {
        try {
            $params = $request->get();
            if(empty($params['uid'])) abort(101,'用户id不能为空');
            if(empty($params['date'])) abort(101,'日期不能为空');
            $count = SignLogic::getSignDay($params['uid'],$params['date']);//查看签到天数
            return common::success('操作成功',$count);
        }catch (\Exception $exception){
            return common::fail($exception->getMessage(),'null');
        }
    }

    /**
     * 查看连续签到天数
     * @param Request $request
     * @return \think\response\Json
     * @yuankai
     */
    public function getField(Request $request)
    {
        try {
            $uid = $request->get('uid');
            if(empty($uid)) abort(101,'用户id不能为空');
            $date = $request->get('date');
            if(empty($date)) abort(101,'日期不能为空');
            $continuous = SignLogic::getField($uid,$date);//查看连续签到天数
            return common::success('操作成功',$continuous);
        }catch (\Exception $exception){
            return common::fail($exception->getMessage(),'null');
        }

    }
}
